let ls
let ss

if (!ls && globalThis.localStorage) ls = window.localStorage
if (!ss && globalThis.sessionStorage) ss = window.sessionStorage

export const findLocal = (regx) => {
  return Object.keys(ls).filter(key => regx.test(key)).map(key => getLocal(key))
}

export const setLocal = (key, value) => {
  if (ls && key && value) {
    if (typeof value === 'object') {
      value = JSON.stringify(value)
      value = 'obj-' + value
    } else {
      value = 'str-' + value
    }
    ls.setItem(key, value)
  }
}

export const getLocal = key => {
  if (ls) {
    let v = ls.getItem(key)
    if (!v) return
    if (v.indexOf('obj-') === 0) {
      v = v.slice(4)
      return JSON.parse(v)
    } else if (v.indexOf('str-') === 0) {
      return v.slice(4)
    } else {
      return v
    }
  }
}

export const rmLocal = key => {
  if (ls && key) {
    ls.removeItem(key)
  }
}

export const clearLocal = () => {
  ls && ls.clear()
}

export const findSession = (regx) => {
  return Object.keys(ss).filter(key => regx.test(key)).map(key => getSession(key))
}

export const setSession = (key, value) => {
  if (ss && key && value) {
    if (typeof value === 'object') {
      value = JSON.stringify(value)
      value = 'obj-' + value
    } else {
      value = 'str-' + value
    }
    ss.setItem(key, value)
  }
}

export const getSession = key => {
  if (ss) {
    let v = ss.getItem(key)
    if (!v) return
    if (v.indexOf('obj-') === 0) {
      v = v.slice(4)
      return JSON.parse(v)
    } else if (v.indexOf('str-') === 0) {
      return v.slice(4)
    } else {
      return v
    }
  }
}

export const rmSession = key => {
  if (ss && key) {
    ss.removeItem(key)
  }
}

export const clearSession = () => {
  ss && ss.clear()
}

/**
 * 取URL传参
 * @param name
 * @returns {*}
 */
export const getQueryString = (name) => {
  const reg = new RegExp(`(^|&)${name}=([^&]*)(&|$)`)
  const str = window.location.search.substr(1).match(reg)
  if (str !== null) {
    return decodeURI(str[2])
  }
  return null
}

/**
 * base64转blob
 * @param {*} code
 * @returns
 */
export const base64ToBlob = function(code) {
  const [fileType, rawFile] = code.split(';base64,')
  const contentType = fileType.split(':')[1]
  const raw = window.atob(rawFile)

  let uInt8Array = new Uint8Array(raw.length)
  for (let i = 0; i < raw.length; i++) {
    uInt8Array[i] = raw.charCodeAt(i)
  }
  return new Blob([uInt8Array, { type: contentType }])
}

export const env = () => {
  const inBrowser = typeof window !== 'undefined'

  const UA = inBrowser && window.navigator.userAgent.toLowerCase()

  const isIE9 = UA && UA.indexOf('msie 9.0') > 0
  const isMobile = UA.match(/(mobile|iphone|ipod|ios|android|blackberry|iemobile|huawei|xiaomi)/i) && !UA.match(/pad/i)
  const inBdapp = /swan\//.test(UA) || /^webswan-/.test(window.name)
  const inWxapp = /miniProgram/i.test(UA)
  const inWxBrowser = /microMessenger/i.test(UA)
  const inBdBrowser = /baiduboxapp/i.test(UA)
  const inByBrowser = /BytedanceWebview/i.test(UA)
  const inByapp = /toutiaoMicroApp/i.test(UA)
  const inMiniapp = inBdapp || inWxapp || inWxBrowser || inBdBrowser || inByapp
  return {
    inBrowser,
    isMobile,
    isIE9,
    inBdapp,
    inWxapp,
    inWxBrowser,
    inBdBrowser,
    inByBrowser,
    inMiniapp
  }
}


// export const env = () => {
//   const inBrowser = typeof window !== 'undefined'
//   const UA = inBrowser && window.navigator.userAgent.toLowerCase()
//   const isIE = UA && /msie|trident/.test(UA)
//   const isIE9 = UA && UA.indexOf('msie 9.0') > 0
//   const isEdge = UA && UA.indexOf('edge/') > 0
//   const isAndroid = (UA && UA.indexOf('android') > 0)
//   const isIOS = (UA && /iphone|ipad|ipod|ios/.test(UA))
//   const isChrome = UA && /chrome\/\d+/.test(UA) && !isEdge
//   const isPhantomJS = UA && /phantomjs/.test(UA)
//   const inDesktop = inBrowser && window.screen.width >= 768 && !UA.match(/applewebkit.*mobile.*/)
//   const inAppIOS = UA && /cnhnb_ios/.test(UA)
//   const inAppAndroid = UA && /cnhnb_android_/.test(UA)
//   const inApp = inAppIOS || inAppAndroid
//   const inBdapp = /swan\//.test(UA) || /^webswan-/.test(window.name)
//   const inWxapp = /miniProgram/i.test(UA)
//   const inWxBrowser = /microMessenger/i.test(UA)
//   const inBdBrowser = /baiduboxapp/i.test(UA)
//   const inMiniapp = inBdapp || inWxapp || inWxBrowser || inBdBrowser
//   return {
//     inBdapp,
//     inWxapp,
//     inWxBrowser,
//     inBdBrowser,
//     inDesktop,
//     inBrowser,
//     inApp,
//     inAppIOS,
//     inAppAndroid,
//     UA,
//     isIE,
//     isIE9,
//     isEdge,
//     isAndroid,
//     isIOS,
//     isChrome,
//     isPhantomJS,
//     inMiniapp
//   }
// }

